{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world!\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello world!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 嵌套循环"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 例子1. 九九乘法表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "# 打印9行\n",
    "for i in range(1,10):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1*1=1\t1*2*1=2\t1*3*1=3\t1*4*1=4\t1*5*1=5\t1*6*1=6\t1*7*1=7\t1*8*1=8\t1*9*1=9\t"
     ]
    }
   ],
   "source": [
    "# 打印1行 1*1 ~ 1*9\n",
    "for j in range(1,10):\n",
    "    print(f\"1*{j}={1*j}\",end=\"\\t\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1\t1*2=2\t1*3=3\t1*4=4\t1*5=5\t1*6=6\t1*7=7\t1*8=8\t1*9=9\t\n",
      "2*1=2\t2*2=4\t2*3=6\t2*4=8\t2*5=10\t2*6=12\t2*7=14\t2*8=16\t2*9=18\t\n",
      "3*1=3\t3*2=6\t3*3=9\t3*4=12\t3*5=15\t3*6=18\t3*7=21\t3*8=24\t3*9=27\t\n",
      "4*1=4\t4*2=8\t4*3=12\t4*4=16\t4*5=20\t4*6=24\t4*7=28\t4*8=32\t4*9=36\t\n",
      "5*1=5\t5*2=10\t5*3=15\t5*4=20\t5*5=25\t5*6=30\t5*7=35\t5*8=40\t5*9=45\t\n",
      "6*1=6\t6*2=12\t6*3=18\t6*4=24\t6*5=30\t6*6=36\t6*7=42\t6*8=48\t6*9=54\t\n",
      "7*1=7\t7*2=14\t7*3=21\t7*4=28\t7*5=35\t7*6=42\t7*7=49\t7*8=56\t7*9=63\t\n",
      "8*1=8\t8*2=16\t8*3=24\t8*4=32\t8*5=40\t8*6=48\t8*7=56\t8*8=64\t8*9=72\t\n",
      "9*1=9\t9*2=18\t9*3=27\t9*4=36\t9*5=45\t9*6=54\t9*7=63\t9*8=72\t9*9=81\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    for j in range(1,10):\n",
    "        print(f\"{i}*{j}={i*j}\",end=\"\\t\")\n",
    "    print(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "2*1=2\t\n",
      "3*1=3\t3*2=6\t\n",
      "4*1=4\t4*2=8\t4*3=12\t\n",
      "5*1=5\t5*2=10\t5*3=15\t5*4=20\t\n",
      "6*1=6\t6*2=12\t6*3=18\t6*4=24\t6*5=30\t\n",
      "7*1=7\t7*2=14\t7*3=21\t7*4=28\t7*5=35\t7*6=42\t\n",
      "8*1=8\t8*2=16\t8*3=24\t8*4=32\t8*5=40\t8*6=48\t8*7=56\t\n",
      "9*1=9\t9*2=18\t9*3=27\t9*4=36\t9*5=45\t9*6=54\t9*7=63\t9*8=72\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    for j in range(1,i):\n",
    "        print(f\"{i}*{j}={i*j}\",end=\"\\t\")\n",
    "    print(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 例子2 打印*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*****\n"
     ]
    }
   ],
   "source": [
    "# 打印一行5个星\n",
    "for i in range(5):\n",
    "    print(\"*\",end=\"\")\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*****\n",
      "*****\n",
      "*****\n",
      "*****\n",
      "*****\n"
     ]
    }
   ],
   "source": [
    "# 打印5行5个星\n",
    "for j in range(5):\n",
    "    for i in range(5):\n",
    "        print(\"*\",end=\"\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*\n",
      "**\n",
      "***\n",
      "****\n",
      "*****\n"
     ]
    }
   ],
   "source": [
    "# 打印*\n",
    "for j in range(5):\n",
    "    for i in range(j+1):\n",
    "        print(\"*\",end=\"\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*****\n",
      "****\n",
      "***\n",
      "**\n",
      "*\n"
     ]
    }
   ],
   "source": [
    "for j in range(5):\n",
    "    for i in range(5-j):\n",
    "        print(\"*\",end=\"\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 例子3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "上第1节课\n",
      "上第2节课\n",
      "上第3节课\n",
      "上第4节课\n",
      "上第5节课\n",
      "上第6节课\n",
      "上第7节课\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,8):\n",
    "    print(f\"上第{i}节课\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "星期1\n",
      "\t上第1节课\n",
      "\t上第2节课\n",
      "\t上第3节课\n",
      "\t上第4节课\n",
      "\t上第5节课\n",
      "\t上第6节课\n",
      "\t上第7节课\n",
      "快乐的一天结束了\n",
      "星期2\n",
      "\t上第1节课\n",
      "\t上第2节课\n",
      "\t上第3节课\n",
      "\t上第4节课\n",
      "\t上第5节课\n",
      "\t上第6节课\n",
      "\t上第7节课\n",
      "快乐的一天结束了\n",
      "星期3\n",
      "\t上第1节课\n",
      "\t上第2节课\n",
      "\t上第3节课\n",
      "\t上第4节课\n",
      "\t上第5节课\n",
      "\t上第6节课\n",
      "\t上第7节课\n",
      "快乐的一天结束了\n",
      "星期4\n",
      "\t上第1节课\n",
      "\t上第2节课\n",
      "\t上第3节课\n",
      "\t上第4节课\n",
      "\t上第5节课\n",
      "\t上第6节课\n",
      "\t上第7节课\n",
      "快乐的一天结束了\n",
      "快乐的一周结束了\n"
     ]
    }
   ],
   "source": [
    "print(\"快乐的一周开始了\")\n",
    "for j in range (1,5):\n",
    "    print(f\"星期{j}\")\n",
    "    for i in range(1,8):\n",
    "        print(f\"\\t上第{i}节课\")\n",
    "    print(\"快乐的一天结束了\")\n",
    "print(\"快乐的一周结束了\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
